package com.mybatis.sqlsession;

import com.mybatis.entity.Configuration;
import com.mybatis.entity.MappedStatement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

/**
 * 创建人: yyn
 * 创建时间:2021/7/9 22:16
 */
public class SimpleExecutor implements Executor {
    private Configuration configuration;

    public SimpleExecutor(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <T> List<T> query(MappedStatement mappedStatement, Object... param) throws Exception {
        StatementHandler statementHandler = new SimpleStatementHandler(configuration);
        PreparedStatement preparedStatement = statementHandler.prepareStatement(mappedStatement,param);
        ResultSetHandler resultSetHandler = new SimpleResultSetHandler(configuration);
        return resultSetHandler.handleResult(preparedStatement,mappedStatement);
    }

    @Override
    public int doUpdate(MappedStatement mappedStatement, Object... param) throws Exception {
        StatementHandler statementHandler = new SimpleStatementHandler(configuration);
        PreparedStatement preparedStatement = statementHandler.prepareStatement(mappedStatement,param);
        return preparedStatement.executeUpdate();
    }
}
